﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AAA.SerialPort.Printer
{
    public class DefaultCommand : ICommand
    {
        public const string NEW_LINE = "NewLine";
        public const string NEW_PAGE = "NewPage";
        public const string CUT_RESERVE_1 = "CutReserve1";
        public const string CUT_RESERVE_3 = "CutReserve3";
        public const string NEW_PAGE_CUT_RESERVE_1 = "NewPageCutReserve1";
        public const string NEW_PAGE_CUT_RESERVE_3 = "NewPageCutReserve3";
        public const string BACK_TO_FIRST = "BackToFirst";
        public const string DOUBLE_WIDTH = "DoubleWidth";
        public const string UNDERLINE = "UnderLine";
        public const string CANCEL = "Cancel";
        public const string RESET_PRINTER = "ResetPrinter";
        public const string ONLY_STUB = "OnlyStub";
        public const string ONLY_RECEIVER = "OnlyReceiver";
        public const string STUB_AND_RECEIVER = "StubAndReceiver";
        public const string MOVE_LINE = "MoveLine";
        public const string PRINT_MARK = "PrintMark";
        public const string OPEN_CASH_DRAWER1 = "OpenCashDrawer1";
        public const string OPEN_CASH_DRAWER2 = "OpenCashDrawer2";
        public const string CHINESE_FONT_ON = "ChineseFontOn";
        public const string CHINESE_FONT_OFF = "ChineseFontOff";
        public const string FONT_SIZE_0 = "FontSize0";
        public const string FONT_SIZE_16 = "FontSize16";
        public const string FONT_SIZE_32 = "FontSize32";
        public const string FONT_SIZE_48 = "FontSize48";

        private Dictionary<string, byte[]> _dicCommand;

        public DefaultCommand()
        {
            _dicCommand = new Dictionary<string, byte[]>();
        }

        protected void SetCommand(string strCommand, byte[] bCommand)
        {
            if (_dicCommand.ContainsKey(strCommand))
                _dicCommand[strCommand] = bCommand;
            else
                _dicCommand.Add(strCommand, bCommand);
        }

        protected byte[] GetCommand(string strCommand)
        {
            return _dicCommand.ContainsKey(strCommand)
                    ? _dicCommand[strCommand]
                    : null;
        }

        #region ICommand 成員

        public byte[] NewLine
        {
            get { return GetCommand(NEW_LINE); }
        }

        public byte[] NewPage
        {
            get { return GetCommand(NEW_PAGE); }
        }

        public byte[] CutReserve1
        {
            get { return GetCommand(CUT_RESERVE_1); }
        }

        public byte[] CutReserve3
        {
            get { return GetCommand(CUT_RESERVE_3); }
        }

        public byte[] NewPageCutReserve1
        {
            get { return GetCommand(NEW_PAGE_CUT_RESERVE_1); }
        }

        public byte[] NewPageCutReserve3
        {
            get { return GetCommand(NEW_PAGE_CUT_RESERVE_3); }
        }

        public byte[] BackToFirst
        {
            get { return GetCommand(BACK_TO_FIRST); }
        }

        public byte[] DoubleWidth
        {
            get { return GetCommand(DOUBLE_WIDTH); }
        }

        public byte[] Underline
        {
            get { return GetCommand(UNDERLINE); }
        }

        public byte[] Cancel
        {
            get { return GetCommand(CANCEL); }
        }

        public byte[] ResetPrinter
        {
            get { return GetCommand(RESET_PRINTER); }
        }

        public byte[] OnlyStub
        {
            get { return GetCommand(ONLY_STUB); }
        }

        public byte[] OnlyReceiver
        {
            get { return GetCommand(ONLY_RECEIVER); }
        }

        public byte[] StubAndReceiver
        {
            get { return GetCommand(STUB_AND_RECEIVER); }
        }

        public byte[] MoveLines(int iLines)
        {
            byte[] bCommand = GetCommand(MOVE_LINE);

            if (bCommand == null)
                return bCommand;

            byte[] bReturns = new byte[bCommand.Length + 1];

            Array.Copy(bCommand, bReturns, bCommand.Length);
            bReturns[bReturns.Length - 1] = (byte)iLines;
            return bReturns;
        }

        public byte[] PrintMark
        {
            get { return GetCommand(PRINT_MARK); }
        }

        public byte[] OpenCashDrawer1
        {
            get { return GetCommand(OPEN_CASH_DRAWER1); }
        }

        public byte[] OpenCashDrawer2
        {
            get { return GetCommand(OPEN_CASH_DRAWER2); }
        }

        public byte[] TransferCommand(object oCommand)
        {
            return GetCommand((string)oCommand);
        }

        public bool SendCommand(System.IO.Ports.SerialPort printer, byte[] bCommand)
        {
            try
            {
                if (bCommand == null)
                    return false;

                if (printer.IsOpen == false)
                    return false;

                printer.Write(bCommand, 0, bCommand.Length);

                return true;
            }
            catch (Exception ex)
            {
                throw;
            }
            return false;
        }

        #endregion
    }
}
